package com.ming.rabbitmq.consumer.simple;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

public class Main {
    public static void main(String[] args) throws IOException, TimeoutException {
        HashMap<Integer,Integer> map = new HashMap();
        String s="abc";
        map.containsKey(s.charAt(0));
        new Integer(s.charAt(0));

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("119.91.238.104");
        factory.setPort(5672);
        factory.setVirtualHost("/simple");
        factory.setUsername("admin");
        factory.setPassword("admin");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // 创建队列
        // 队列名称,是否持久化,消费者是否独占,是否自动删除,参数
        channel.queueDeclare("simple",true,false,false,null);

        // 接收消息
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] message) throws IOException {
                System.out.printf("consumerTag:%s\n", consumerTag);
                System.out.printf("Exchange:%s\n", envelope.getExchange());
                System.out.printf("RoutingKey:%s\n", envelope.getRoutingKey());
                System.out.printf("properties:%s\n", properties);
                System.out.printf("message:%s\n", new String(message));
            }
        };
        channel.basicConsume("simple",true,consumer);
    }
}
